package com.wxisme.ssm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wxisme.ssm.po.Album;
import com.wxisme.ssm.po.Friend;
import com.wxisme.ssm.po.Photo;
import com.wxisme.ssm.po.UserInfo;
import com.wxisme.ssm.service.AlbumService;
import com.wxisme.ssm.service.FriendService;
import com.wxisme.ssm.service.PhotoService;
import com.wxisme.ssm.service.UserInfoService;

/**
 *friend
 *@author wxisme
 *@time 2015-10-3 上午10:30:12
 */
@Controller
public class FriendController {
	
	@Autowired
	private UserInfoService userInfoService;
	
	@Autowired
	private FriendService friendService;
	
	@Autowired
	private AlbumService albumService;
	
	@Autowired
	private PhotoService photoService;
	
	
	/**
	 * 通过用户名来查找朋友
	 * @param request
	 * @param session
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("findFriend.action")
	public ModelAndView findFriends(HttpServletRequest request, HttpSession session) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		
		String friendName = request.getParameter("findusername");
		
		UserInfo userInfo = userInfoService.findFriends(friendName);
		
		if(userInfo == null) {
			modelAndView.addObject("findFriend", "0");
			modelAndView.setViewName("searchFriend");
		}
		else {
			
			//添加到session，添加好友的时候要用到
			session.setAttribute("findedFriend", userInfo.getUsername());
			modelAndView.addObject("fridend", userInfo);
			modelAndView.setViewName("addFriend");
		}
		
		return modelAndView;
	}
	
	/**
	 * 添加朋友Controller
	 * @param request
	 * @param session
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/addFriend.action")
	public ModelAndView addFriend(HttpServletRequest request, HttpSession session) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		
		String findedFriendName = (String) session.getAttribute("findedFriend");
		session.removeAttribute("findedFriend");//获取之后移除
		String myname = (String) session.getAttribute("username");
		
		Friend friend = new Friend();
		friend.setFriendname(findedFriendName);
		friend.setMyname(myname);
		long time = (new Date()).getTime();
		friend.setTime(time);
		
		Friend friend2 = new Friend();
		friend2.setFriendname(myname);
		friend2.setMyname(findedFriendName);
		friend2.setTime(time);
		//互为好友
		friendService.insertFriend(friend);
		friendService.insertFriend(friend2);
		
		modelAndView.setViewName("personal");
		
		
		return modelAndView;
	}

	/**
	 * 查找好友列表
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryFriends.action")
	public ModelAndView queryFriends(HttpServletRequest request, HttpSession session) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		
		String username = (String) session.getAttribute("username");
		List<Friend> friends = friendService.selectFriends(username);
		
		List<String> heads = new ArrayList<String>();
		
		for(int i=0; i<friends.size(); i++) {
			heads.add(userInfoService.selectHead(friends.get(i).getFriendname()));
		}
		
		
		modelAndView.addObject("heads", heads);
		modelAndView.addObject("friends", friends);
		modelAndView.setViewName("shareSpace");
		
		
		return modelAndView;
	}
	
	/**
	 * 查找好友相册
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/seeFriendAlbum.action")
	public ModelAndView seeFriendAlbum(HttpServletRequest request, HttpSession session) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		
		String friendname = request.getParameter("friendname");
		String headPath = request.getParameter("headPath");
		
		
		//查找相册
		List<Album> friendAlbumList = albumService.selectAlbumByFriendname(friendname);
		
		Map<Album, List<Photo>> friendLoadingMap = new HashMap<Album, List<Photo>>();
		
		for(int i=0; i<friendAlbumList.size(); i++) {
			Album album = friendAlbumList.get(i);
			List<Photo> photos = photoService.selectPhotoByAlbumPath(album.getPathName());
			friendLoadingMap.put(album, photos);
		}
		
		
		session.setAttribute("friendname", friendname);
		session.setAttribute("headPath", headPath);
		session.setAttribute("friendLoadingMap", friendLoadingMap);
		
		modelAndView.setViewName("displayFriendAlbums");
		return modelAndView;
	}
	
	
	/**
	 * 删除好友
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteFriend.action")
	public ModelAndView deleteFriend(HttpSession session) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		
		String friendname = (String) session.getAttribute("friendname");
		
		String myname = (String) session.getAttribute("username");
		
		Friend friend = new Friend();
		
		friend.setMyname(myname);
		friend.setFriendname(friendname);
		
		friendService.deleteFriend(friend);
		
		modelAndView.setViewName("personal");
		
		return modelAndView;
	}
	
	
	
	
	
	
	
	
}
